﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using mdryden.Core.Localization;

public static class BreadcrumbHelper
{

	private static void AddBreadCrumb(HtmlHelper helper, string text)
	{
		var crumbs = helper.ViewData["BreadCrumbs"] as List<string>;

		if (crumbs == null)
		{
			crumbs = new List<string>();
		}

		crumbs.Add(text);

		helper.ViewData["BreadCrumbs"] = crumbs;
	}

	private static void AddBreadCrumb(HtmlHelper helper, string text, string url)
	{
		const string anchorFormat = "<a href='{0}'>{1}</a>";

		var anchor = string.Format(anchorFormat, url, text);

		AddBreadCrumb(helper, anchor);
	}

	public static void AddCurrentBreadCrumb(this WebViewPage page, string text)
	{
		// don't add this item if we're on mobile
		if (!page.Html.IsMobile())
		{
			AddBreadCrumb(page.Html, text);
		}
	}

	public static void AddIndexBreadCrumb(this WebViewPage page)
	{
		AddBreadCrumb(page.Html, LanguageLoader.GetWord("ForumIndexBreadCrumbText"), page.Url.Action("Index", "Forum"));
	}

	public static void AddForumBreadCrumb(this WebViewPage page, int forumID)
	{
		if (page.ViewBag.ForumTitle == null)
		{
			throw new Exception("ViewBag.ForumTitle is null");
		}

		AddBreadCrumb(page.Html, (string)page.ViewBag.ForumTitle, page.Url.Action("ViewForum", "Forum", new { forumID = forumID }));
	}

	public static void AddUserBreadCrumb(this WebViewPage page, string username)
	{

		if (string.IsNullOrEmpty(username))
		{
			throw new Exception("Username is null");
		}

		AddBreadCrumb(page.Html, username, page.Url.Action("ViewUser", "User", new { username = username }));
	}

	public static void AddUserListBreadCrumb(this WebViewPage page)
	{
		AddBreadCrumb(page.Html, LanguageLoader.GetWord("ViewUserListBreadcrumbText"), page.Url.Action("List", "User"));
	}

	public static void AddActionLinkBreadCrumb(this WebViewPage page, MvcHtmlString action)
	{
		AddBreadCrumb(page.Html, action.ToHtmlString());		
	}

}